package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.example.entity.Recruitmentrequirement;
import org.example.models.R;
import org.example.service.RecruitmentrequirementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/recruitmentBasics")
public class RecruitmentBasicsController {
    @Autowired
    private RecruitmentrequirementService recruitmentBasicsService;

    @GetMapping("/find") // 查询
    public R<Page<Recruitmentrequirement>> find(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size, // 每页记录数
            @RequestParam(value = "JobName", required = false) String JobName,// 职位名称
            @RequestParam(value = "status", required = false) String status // 职位状态
    ) {
        QueryWrapper<Recruitmentrequirement> queryWrapper = new QueryWrapper<>(); // 创建查询条件对象
        QueryWrapper<Recruitmentrequirement> jobname1 = queryWrapper.like(JobName != null, "JobName", JobName); // 职位名称模糊查询
        queryWrapper.eq(status != null, "status", status);
        Page<Recruitmentrequirement> page = new Page<>(current, size); // 创建分页对象

        return R.success(recruitmentBasicsService.page(page, jobname1));

    }

    @PostMapping("insert") // 添加
    public R insert(@RequestBody Recruitmentrequirement recruitmentBasics) {

        boolean save = recruitmentBasicsService.save(recruitmentBasics);
        if (save) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    //根据需求id查询
    @PostMapping("/findById")
    public R<Recruitmentrequirement> findById(@RequestParam("requirementid") Integer requirementid) {
        Recruitmentrequirement byId = recruitmentBasicsService.getById(requirementid);
        return R.success(byId);
    }

    // 根据id进行删除
    @PostMapping("/deleteById")
    public R deleteById(@RequestParam("requirementid") Integer requirementid) {
        Recruitmentrequirement byId = recruitmentBasicsService.getById(requirementid);
        return R.success(recruitmentBasicsService.removeById(byId));
    }
    
    // 根据id进行修改
    @PostMapping("/updateById")
    public R updateById(@RequestBody Recruitmentrequirement recruitmentBasics) {
        return R.success(recruitmentBasicsService.updateById(recruitmentBasics));
    }

    //根据时间升序排序
    @GetMapping("/sortByTime")
    public R<Page<Recruitmentrequirement>> sortByTime(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size, // 每页记录数
            @RequestParam(value = "JobName", required = false) String JobName, // 职位名称
            @RequestParam(value = "status", required= false)String status// 状态
    ) {
        QueryWrapper<Recruitmentrequirement> queryWrapper = new QueryWrapper<>();// 创建查询条件对象

        if (JobName != null && !JobName.isEmpty()) {
            queryWrapper.like("jobName", JobName);
        }
        queryWrapper.eq(status != null, "status", status); // 状态匹配
        Page<Recruitmentrequirement> page = new Page<>(current, size); // 创建分页对象
        return R.success(recruitmentBasicsService.page(page, queryWrapper.orderByAsc("creationdate")));
    }

    //根据时间降序排序
    @GetMapping("/sortByTimeDesc")
    public R<Page<Recruitmentrequirement>> sortByTimeDesc(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size, // 每页记录数
            @RequestParam(value = "JobName", required = false) String JobName, // 职位名称
            @RequestParam(value = "status", required= false)String status // 状态
    ) {
        QueryWrapper<Recruitmentrequirement> queryWrapper = new QueryWrapper<>(); // 创建查询条件对象
        if (JobName != null && !JobName.isEmpty()) {
            queryWrapper.like("JobName", JobName);
        }
        queryWrapper.eq(status != null, "status", status); // 状态匹配
        Page<Recruitmentrequirement> page = new Page<>(current, size); // 创建分页对象
        return R.success(recruitmentBasicsService.page(page, queryWrapper.orderByDesc("creationdate"))); // 降序排序
    }

    // 根据状态查询（模糊）
    @PostMapping("/findByStatus")
    public R<Page<Recruitmentrequirement>> findByStatus(
            @RequestParam(value = "current", defaultValue = "1") int current, // 当前页码
            @RequestParam(value = "size", defaultValue = "10") int size,     // 每页记录数
            @RequestParam(value = "JobName", required = false) String JobName, // 职位名称
            @RequestParam(value = "status", required = false) String status    // 职位状态
    ) {
        QueryWrapper<Recruitmentrequirement> queryWrapper = new QueryWrapper<>();

        if (JobName != null && !JobName.isEmpty()) {
            queryWrapper.like("JobName", JobName); // 模糊查询
        }
       // 如果status 为全部则不添加状态筛选条件
        if(Objects.equals(status, "全部")){
    // 不添加状态条件，查询所有状态的数据
    }else if(status != null && !status.isEmpty()){
    queryWrapper.eq("status", status); // 状态匹配
}

        Page<Recruitmentrequirement> page = new Page<>(current, size); // 创建分页对象


        return R.success(recruitmentBasicsService.page(page, queryWrapper)); // 返回查询结果
    }

    // 根据状态查询
    @GetMapping("/findByStatus1")
    public R<List<Recruitmentrequirement>> findByStatus(
    ) {
        String jobname = "进行中";
        List<Recruitmentrequirement> byStatus1 = recruitmentBasicsService.findByStatus1(jobname);
        if (byStatus1 != null) {
            return R.success(byStatus1);
        }
        return R.success("查询失败");

    }

//通过需求ID和HR负责人ID查找对应的招聘需求记录
    @GetMapping("/selectByRequirementIdAndRecruiterId")
    public R<List<Recruitmentrequirement>> selectByRequirementIdAndRecruiterId(@RequestParam("requirementid") String requirementid,

                                                                                @RequestAttribute("userId") String userId) {

        String recruiterid = userId;
        List<Recruitmentrequirement> byRequirementIdAndRecruiterId = recruitmentBasicsService.selectByRequirementIdAndRecruiterId(requirementid, recruiterid);
        if (byRequirementIdAndRecruiterId != null) {
            return R.success(byRequirementIdAndRecruiterId);
        }
        return R.success("查询失败");
    }
}
